home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex2.c < prev    next >
C/C++ Source or Header  |  1995-04-22  |  4KB  |  86 lines

  1. #include <genstub.c>
  2.  
  3. // Add these menu options.
  4. #define IDM_READ    201
  5. #define IDM_WRITE   202
  6.  
  7. // Windows message procedure.
  8. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10.     static HANDLE hSemRead = 0;         // Handle of the read semaphore.
  11.     static HANDLE hEventWrite = 0;      // Handle of the write event.
  12.     switch (uMsg)
  13.     {
  14.             case WM_CREATE: // Make the two synchornization objects.
  15.                     {
  16.                        hSemRead = CreateSemaphore( NULL, 10, 10 "READ SEMAPHORE" );
  17.                        hEventWrite = CreateEvent( NULL, TRUE, TRUE, "WRITE EVENT" );
  18.                        return DefWindowProc( hWnd, uMsg, wParam, lParam );
  19.                     }
  20.                     break;
  21.             case WM_DESTROY: // Close the synchonization objects.
  22.                     if ( hSemRead )
  23.                        CloseHandle( hSemRead );
  24.                     if ( hEventWrite )
  25.                        CloseHandle( hEventWrite );
  26.                     PostQuitMessage( 0 );
  27.                     break;
  28.             case WM_COMMAND:       // Process menu items
  29.                     switch ( LOWORD( wParam )  )
  30.                     {
  31.                        case IDM_READ:
  32.                             hSemRead = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE,
  33.                                                       "Read Semaphore" );
  34.                             hEventWrite = OpenEvent( SYNCHRONIZE, FALSE, "Write Event" );
  35.                             SetWindowText( hWnd, "Waiting for all Writes to Finish" );
  36.                             // Check that write manual reset event is signaled.
  37.                             WaitForSingleObject( hEventWrite, INFINITE );
  38.                             // Wait for semaphore.
  39.                             WaitForSingleObject( hSemRead, INFINITE );
  40.                             // Do the read.
  41.                             SetWindowText( hWnd, "Reading" );
  42.                             Sleep( 5000 );    // Sleep instead to dramatize contention.
  43.                             // Release semaphore.
  44.                             ReleaseSemaphore( hSemRead, 1, NULL );
  45.                             SetWindowText( hWnd, "Done Reading" );
  46.                             CloseHandle( hSemRead );
  47.                             CloseHandle( hEventWrite );
  48.                         break;
  49.                         case IDM_WRITE:
  50.                         {
  51.                             DWORD dwSemaphoreCount = 0;
  52.                             hSemRead = OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE,
  53.                                                       "Read Semaphore" );
  54.                             hEventWrite = OpenEvent( SYNCHRONIZE, FALSE, "Write Event" );
  55.                             // Wait for manual reset event object: it becomes non-signaled.
  56.                             SetWindowText( hWnd, "Waiting for Write Event" );
  57.                             WaitForSingleObject( hEventWrite, INFINITE );
  58.                             ResetEvent( hEventWrite );
  59.                             SetWindowText( hWnd, "Waiting for All Reads to Finish" );
  60.                             // Get semaphore, Release semaphore until its count is maximum.
  61.                             while ( dwSemaphoreCount!=SEMAPHORE_MAX_COUNT )
  62.                             {
  63.                                 WaitForSingleObject( hSemRead, INFINITE );
  64.                                 ReleaseSemaphore( hSemRead, 1, &dwSemaphoreCount );
  65.                                 dwSemaphoreCount++;
  66.                             }
  67.                             SetWindowText( hWnd, "Writing" );
  68.                             // Do the write.
  69.                             Sleep( 10000 );  // Sleep instead to dramatize contention.
  70.                             SetWindowText( hWnd, "Done Writing" );
  71.                             // SetEvent: it becomes signaled.
  72.                             SetEvent( hEventWrite );
  73.                             CloseHandle( hSemRead );
  74.                             CloseHandle( hEventWrite );
  75.                         }
  76.                         break;
  77.                         case IDM_EXIT:
  78.                             DestroyWindow( hWnd );
  79.                             break;
  80.                     }
  81.             break;
  82.             default:
  83.                  return DefWindowProc( hWnd, uMsg, wParam, lParam );
  84.     }
  85.     return NULL;
  86. }